<?php


// ----------------------------------------------------------------------------
// --- [ Smarty class ] -------------------------------------------------------
// ----------------------------------------------------------------------------

chdir(dirname(__FILE__)."/..");
$pth = getcwd();
chdir(dirname(__FILE__));
$_cfg = parse_ini_file(dirname(__FILE__).'/../cfg/config.php', 1);
$_cfg["file_root"] = $pth;
$_cfg["system"]["file_root"] = $pth;
$_cfg["http_root"] = $cfg["system"]["http_root"];

require ('smarty/Smarty.class.php');
    
/**
 * Exemplar of Smarty class
 *
 */
class coreSmarty extends Smarty
{
    function coreSmarty($arg = array(
                            "template_dir" => "",
                            "compile_dir" => "",
                            "config_dir" => "",
                            "cache_dir" => "",
                            ))
    {
        global $_cfg;

        $this->Smarty();

        $this->template_dir     = str_replace("//", "/", $_cfg['file_root'].("" == $arg['template_dir'] ? $_cfg['system']['template_dir'] : $arg['template_dir']));
        $this->compile_dir      = str_replace("//", "/", $_cfg['file_root'].("" == $arg['compile_dir'] ? $_cfg['system']['compile_dir'] : $arg['compile_dir']));
        $this->config_dir       = str_replace("//", "/", $_cfg['file_root'].("" == $arg['config_dir'] ? $_cfg['system']['config_dir'] : $arg['config_dir']));
        $this->cache_dir        = str_replace("//", "/", $_cfg['file_root'].("" == $arg['cache_dir'] ? $_cfg['system']['cache_dir'] : $arg['cache_dir']));
        
        unset($_cfg);

        $this->caching = false;
        
        //$this->register_modifier("geturl", "smarty_geturl_modifier");
        $this->register_function("geturl",  "smarty_geturl_function");
        $this->register_function("getsurl", "smarty_get_safe_catalog");
        $this->register_function("drawimg", "smarty_check_image_exist");
    }
};

function smarty_check_image_exist($img)
{
    $img = $img["img"];
    $core = &getCoreInstance();
    if ($img != ''){
    if(file_exists($core->config["file_root"].'/'.$img) or 
       file_exists($core->config["file_root"].$img) or 
       file_exists($core->config["file_root"].'/inventory'.$img) or
       file_exists($core->config["file_root"].'/inventory/'.$img) or
       file_exists($core->config["file_root"].'/inventory/uploads'.$img) or
       file_exists($core->config["file_root"].'/inventory/uploads/'.$img) or
       file_exists($core->config["file_root"].'/inventory/uploads/file'.$img) or
       file_exists($core->config["file_root"].'/inventory/uploads/file/'.$img) or

       file_exists($core->config["file_root"].'/inventory/uploads/home_page'.$img) or
       file_exists($core->config["file_root"].'/inventory/uploads/home_page/'.$img) or

       file_exists($core->config["file_root"].'/inventory/uploads/image'.$img) or
       file_exists($core->config["file_root"].'/inventory/uploads/image/'.$img)
       
        ){
    
        echo $img;
    }
    else{
    echo 'no_image.jpg';
    }
}
    else{
    echo 'no_image.jpg';
    }

}

function smarty_geturl_modifier($instr, $args)
{
    $core = &getCoreInstance();

    $path = $core->getPluginPath($instr);
    if("" == $path)
    {
        $result = "./";
    }
    else
    {
        $result = "";
    }
    
    return "GoTo[{$instr}]::::[{$args}]";
}

function smarty_geturl_function($params)
{
//  debug($params);
    $path = "";
    switch ($params["plugin"]) {
        case "_this": {
            $path = $_GET["path"];
        } break;
    
        default: {
            $core = &getCoreInstance();
//          debug($core);
            //die();
            $path = $core->getPluginPath($params["plugin"]);
            
            $path = "" == $path ? "./" : $path;
        } break;
    }
    
    $z = array();
    switch ($params["method"]) {
        case "post": {
            $inarray = $_POST;
            while (list ($k, $v) = each ($inarray)) {
                $z[] = "{$k}={$v}";
            }
        } break;

        case "request": {
            $inarray = $_REQUEST;
            unset($inarray["path"]);
            while (list ($k, $v) = each ($inarray)) {
                $z[] = "{$k}={$v}";
            }
        } break;
        
        case "null": {
//          echo "Yo";
        } break;

        case "get":
        default: {
            $inarray = $_GET;
            unset($inarray["path"]);
            while (list ($k, $v) = each ($inarray)) {
                    $z[] = "{$k}={$v}";
            }
        } break;
    }
    return $core->config["http_root"].str_replace("&&", "&", "index.php?path=".$path."&".join("&", $z)."&".$params["params"]);
}


function smarty_get_safe_catalog($params) {

    $id = $params["id"];
    $bid = $id;
    $idx = 0;
    $root = array();

    $core = &getCoreInstance();
        $db = &$core->getProperty("db");
    while($id>0) {
        $ctg = $db->getRow("select catalog_id, parent_id, h_title, is_category from catalog where catalog_id='{$id}'");
    
            if (!isset($ctg['h_title']) or '' == $ctg['h_title']){
                //debug('Error: no html title for item! need setup.');
                $id = 0;
                $notitle = 'true';
            }
            else{
                if($idx===0 && $ctg["is_category"]==='N') {
                    $url = $ctg["h_title"].".html";
                } else {
                    $url = $ctg["h_title"];
                }
                $root[] = $url;
                $idx++;
                $id = $ctg["parent_id"];
                }
    }
    
    if(!preg_match("(\.html)",$root[0])){
        $root[0] .= ".htm"; 
    }

    $url = $core->config["http_root"]."".join("/", array_reverse(array_values($root)));
    if ('true' == $notitle){
        //return '#';
         
        return (smarty_geturl_function($params).$bid);
    }
    else{
        if($url != $core->config["http_root"].'.htm' && $url != $core->config["http_root"].'.html'){
            return $url;
        }
        else{
            return '#';
        }
    }
}


function &createSmartyInstance($arguments)
{
    return new coreSmarty($arguments);
}

?>